﻿// For an introduction to the Page Control template, see the following documentation:
// http://go.microsoft.com/fwlink/?LinkId=232511
(function () {
    "use strict";

    WinJS.UI.Pages.define("/pages/addpet/addpet.html", {
        // This function is called whenever a user navigates to this page. It
        // populates the page elements with the app's data.
        ready: function (element, options) {
            var addPic = function (storageFile) {
                var fileUrl = URL.createObjectURL(storageFile);
                var picElement = document.getElementById("pic");
                picElement.setAttribute("src", fileUrl);
            };
        

            var Capture = Windows.Media.Capture;
            var Storage = Windows.Storage;
            var livePreview;
            var mediaCapture;

            function init() {
                livePreview = document.getElementById("takepic");
                startCamera();
            };

            function startCamera() {
                mediaCapture = new Capture.MediaCapture();
                mediaCapture.initializeAsync().then(function () {
                    livePreview.src = URL.createObjectURL(mediaCapture);
                    livePreview.play();
                });
            }

            function takePhoto() {
                pics.createFileAsync("photo.jpg", Storage.CreationCollisionOption.generateUniqueName)
                  .then(function (file) {
                      var photoProperties = Windows.Media.MediaProperties.ImageEncodingProperties.createJpeg();

                    mediaCapture.capturePhotoToStorageFileAsync(photoProperties, file);
                  });
            }

            var saveVet = document.getElementById("saveved");
            saveVet.style.display = "none";
            var savePet = document.getElementById("savepet");
            savePet.style.display = "";
            savePet.addEventListener("click", AddPetCodeBehind.submitPet);
            WinJS.Utilities.id("addpic").listen("click", function () {
                var openPicker = Windows.Storage.Pickers.FileOpenPicker();

                openPicker.fileTypeFilter.append("*");
                openPicker.pickSingleFileAsync().then(addPic);
            });
            
            WinJS.Utilities.id("takepic").listen("click", init());
        },

        unload: function () {
            // TODO: Respond to navigations away from this page.
        },

        updateLayout: function (element, viewState, lastViewState) {
            /// <param name="element" domElement="true" />

            // TODO: Respond to changes in viewState.
        }
    });
})();
